<template>
	<view class="container">
		<view 
		class="solid-bottom text-13 flex justify-between align-center padding bg-white"
		@click="toPage('/filePage/pages/productList/productList?type='+type+'&add=1')"
		>
			<view class="text-333 text-bold">
				商品
			</view>
			<view>{{productInfo.name}}</view>
			<view v-if='isShow'  class="flex justify-start align-center"
				>
				<view class="text-767696 margin-right-xs" v-if='!productInfo.name'>
					请选择
				</view>
				<image :src="baseUrl + '/static/right.png'" mode="" style="width: 22rpx;height: 22rpx;"></image>
			</view>
		</view>
		<!-- <view class="solid-bottom bg-white text-13 flex justify-between align-center padding">
			<view class="text-333 flex-sub text-bold">
				商品价格
			</view>
			<view class="flex-treble text-13 flex justify-between align-center">
				<input type="text" value="" class="text-13 f-grow1" placeholder="请填写商品单价" />
				<view class="color">
					元
				</view>
			</view>

		</view> -->
		<view class="solid-bottom bg-white text-13 flex justify-between align-center padding">
			<view class="text-333 flex-sub text-bold">
				团购价格
			</view>
			<view class="flex-treble text-13 flex justify-between align-center">
				<input type="text" v-model="groupPrice" value="" class="text-13 f-grow1" placeholder="请填写团购单价" />
				<view class="color">
					元
				</view>
			</view>

		</view>
		<view class="solid-bottom bg-white text-13 flex justify-between align-center padding">
			<view class="text-333 flex-sub text-bold">
				拼团人数
			</view>
			<view class="flex-treble text-13 flex justify-between align-center">
				<input type="number" v-model="memberCount" value="" class="text-13 f-grow1" placeholder="请填写拼团人数" />
				<view class="color">
					人
				</view>
			</view>
		
		</view>
		<!-- 	<view class="margin-bottom-sm bg-white text-13 flex justify-between align-center padding">
			<view class="text-333 flex-sub text-bold">
				库存
			</view>
			<input type="text" value="" class="text-13 flex-treble" placeholder="请填写库存数" />
		</view> -->
		<view class="solid-bottom text-13 flex justify-between align-center padding bg-white">
			<view class="text-333 text-bold">
				开始日期
			</view>
			<view class="flex justify-start align-center" @click="openDate(1)">
				<view class="text-767696 margin-right-xs">
					{{startTime}}
				</view>
				<image :src="baseUrl + '/static/right.png'" mode="" style="width: 22rpx;height: 22rpx;"></image>
			</view>
		</view>
		<view class="margin-bottom-sm text-13 flex justify-between align-center padding bg-white">
			<view class="text-333 text-bold">
				结束日期
			</view>
			<view class="flex justify-start align-center" @click="openDate(2)">
				<view class="text-767696 margin-right-xs">
					{{endTime}}
				</view>
				<image :src="baseUrl + '/static/right.png'" mode="" style="width: 22rpx;height: 22rpx;"></image>
			</view>
		</view>
		<!-- <view class="margin-bottom-sm bg-white text-13 flex justify-between align-center padding">
			<view class="text-333 flex-sub text-bold">
				可查看角色
			</view>
			<radio-group class="flex-treble flex justify-around align-center" @change="RadioChange">
				<view class="flex justify-start align-center">
					<radio class="radio yellow" :class="radio=='A'?'bg-white':''" :checked="radio=='A'?true:false"
						value="A"></radio>
					<view class="title text-13 margin-left-xs">全部</view>
				</view>
				<view class="flex justify-start align-center">
					<radio class="radio yellow" :class="radio=='B'?'checked':''" :checked="radio=='B'?true:false"
						value="B"></radio>
					<view class="title text-13 margin-left-xs">角色</view>
				</view>
				<view class="flex justify-start align-center">
					<radio class="radio yellow" :class="radio=='B'?'checked':''" :checked="radio=='B'?true:false"
						value="B"></radio>
					<view class="title text-13 margin-left-xs">角色</view>
				</view>
			</radio-group>
		</view> -->
		<button class="padding bg-color text-white margin radius10 text-15" @click="submit">确定</button>
		<w-picker :visible.sync="visible" mode="date" startYear="2021" endYear="2129" 
			:current="true" fields="hour" @confirm="onConfirm($event)" :disabled-after="false" ref="date"></w-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: this.$config.baseUrl,
				imgList: [],
				radio: '',
				startTime: '请选择',
				endTime: '请选择',
				time_type: '',
				productInfo: '',
				productId: '',
				visible: false,
				isShow:false,
				groupPrice:'',
				shopId:'',
				type:'',
				memberCount:''
			};
		},
		 onLoad(options) {
		 	if(options.id){
				this.isShow = true
				this.shopId = options.id
			}
			if(options.type){
				this.type = options.type
			}
		 },
		
		onShow() {
			console.log(uni.getStorageSync('productInfo'))
			if (uni.getStorageSync('productInfo')) {
				this.productInfo = uni.getStorageSync('productInfo')
				this.productId = uni.getStorageSync('productInfo').id
				console.log(this.productInfo)
			}
		},
		
		methods: {
			async ChooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						// console.log(res)
						this.$upload(res.tempFilePaths[0]).catch(req => {
							this.imgList.push(req.data.imgurl)
						})
					}
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '提示',
					content: '确定要删除吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			toPage(url) {
				this.$util.openNewPage(url)
			},
			RadioChange(e) {
				this.radio = e.detail.value
			},

			openDate(e) {
				console.log(e)
				this.visible = true
				this.time_type = e

			},
			async submit() {
				const that = this
				const {
					productId,
					startTime,
					endTime,
					groupPrice,
					shopId,
					memberCount
				} = this
				
				
				
				if (!productId) {
					return this.$util.msg('请选择商品')
				}
				if (!groupPrice) {
					return this.$util.msg('请输入团购价格')
				}
				if(!memberCount){
					return this.$util.msg('请输入拼团人数')
					}else{
						if(memberCount.length==1){
							let regex = /[^2-9]/gi
							if(regex.test(memberCount)){
								return this.$util.msg('拼团人数格式不符合要求')
							}
						}else{
							let regex = /[\D]/gi
							if(regex.test(memberCount)){
								return this.$util.msg('拼团人数格式不符合要求')
							}
						}
						
					}
				if (!startTime) {
					return this.$util.msg('请选择开始时间')
				}
				if (!endTime) {
					return this.$util.msg('请选择结束时间')
				}
				const data = {
					productId,
					groupPrice: groupPrice * 100,
					startTime,
					endTime,
					shopId,
					memberCount
				}
				const [err, rs] = await this.$jsonPost(this.$api.exchangeGroup, data)
				if(rs){
					this.$util.msg(rs.message)
					setTimeout(()=>{
						uni.navigateBack({
							
						})
					},1000)
					
				}

				// uni.showModal({
				//  content: '你确定添加团购商品吗',
				//     success: function (res) {
				//         if (res.confirm) {
				//             console.log('用户点击确定');


				//         } else if (res.cancel) {
				//             console.log('用户点击取消');
				//         }
				//     }
				// });
			},
			onConfirm(e) {
				console.log(e)
				if (this.time_type == 1) {
					this.startTime = e.value
				} else {
					this.endTime = e.value
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.cu-form-group {
		margin-top: 74rpx;
		padding: 0;
	}
</style>
